<!DOCTYPE HTML>
<!--
	Dimension by HTML5 UP
	html5up.net | @ajlkn
	Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
 <head>
  <title>
   Dimension by HTML5 UP
  </title>
  <!-- <meta charset="utf-8" /> -->
  <!-- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> -->
  <meta charset="utf-8"/>
  <meta content="width=device-width,initial-scale=1.0" name="viewport"/>
  <link href="../../assets/css/article.css" rel="stylesheet"/>
  <link href="https://cdn.bootcss.com/highlight.js/9.15.8/styles/github.min.css" rel="stylesheet"/>
  <noscript>
   <link href="../../assets/css/noscript.css" rel="stylesheet"/>
  </noscript>
 </head>
 <body>
  <div id="app">
  </div>
  <!-- built files will be auto injected -->
 </body>
 <body class="is-preload">
  <!-- Wrapper -->
  <div id="wrapper">
   <!-- Main -->
   <div id="main">
    <article id="article">
     <h1 id="java-mongodb">
      Java MongoDB
     </h1>
     <hr/>
     <div class="codehilite">
      <pre><span></span><code><span class="kn">package</span> <span class="nn">cn.nudt681.nssas.utils</span><span class="p">;</span>

<span class="kn">import</span> <span class="nn">com.alibaba.fastjson.JSON</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">com.mongodb.*</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">com.mongodb.client.FindIterable</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">com.mongodb.client.MongoCollection</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">com.mongodb.client.MongoDatabase</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">com.mongodb.client.model.Filters</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.bson.Document</span><span class="p">;</span>

<span class="kn">import</span> <span class="nn">java.util.*</span><span class="p">;</span>

<span class="kn">import static</span> <span class="nn">com.mongodb.client.model.Filters.and</span><span class="p">;</span>
<span class="kn">import static</span> <span class="nn">com.mongodb.client.model.Filters.eq</span><span class="p">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">MongoDBUtil</span> <span class="p">{</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="n">MongoClient</span> <span class="n">mongoClient</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="n">MongoDatabase</span> <span class="n">mongoDatabase</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="n">MongoCollection</span><span class="o">&lt;</span><span class="n">Document</span><span class="o">&gt;</span> <span class="n">collection</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>

    <span class="kd">synchronized</span> <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">init</span><span class="p">()</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">mongoDatabase</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">mongoClient</span> <span class="o">=</span> <span class="k">new</span> <span class="n">MongoClient</span><span class="p">(</span> <span class="s">"172.18.0.24"</span> <span class="p">,</span> <span class="mi">27017</span> <span class="p">);</span>
            <span class="n">mongoDatabase</span> <span class="o">=</span> <span class="n">mongoClient</span><span class="p">.</span><span class="na">getDatabase</span><span class="p">(</span><span class="s">"attack_packets"</span><span class="p">);</span>
            <span class="n">collection</span> <span class="o">=</span> <span class="n">mongoDatabase</span><span class="p">.</span><span class="na">getCollection</span><span class="p">(</span><span class="s">"attack"</span><span class="p">);</span>
            <span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"Connect to database successfully"</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">selectCollection</span><span class="p">(</span><span class="n">String</span> <span class="n">collectionName</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">mongoDatabase</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">init</span><span class="p">();</span>
        <span class="p">}</span>
        <span class="n">collection</span> <span class="o">=</span> <span class="n">mongoDatabase</span><span class="p">.</span><span class="na">getCollection</span><span class="p">(</span><span class="n">collectionName</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">boolean</span> <span class="nf">insert</span><span class="p">(</span><span class="n">Map</span> <span class="n">map</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">try</span> <span class="p">{</span>
            <span class="n">String</span> <span class="n">ip</span> <span class="o">=</span> <span class="p">(</span><span class="n">String</span><span class="p">)</span> <span class="n">map</span><span class="p">.</span><span class="na">get</span><span class="p">(</span><span class="s">"ip"</span><span class="p">);</span>
            <span class="n">Document</span> <span class="n">document</span> <span class="o">=</span> <span class="n">collection</span><span class="p">.</span><span class="na">find</span><span class="p">(</span><span class="n">eq</span><span class="p">(</span><span class="s">"ip"</span><span class="p">,</span> <span class="n">ip</span><span class="p">)).</span><span class="na">first</span><span class="p">();</span>
            <span class="k">if</span><span class="p">(</span><span class="n">document</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">collection</span><span class="p">.</span><span class="na">insertOne</span><span class="p">(</span><span class="k">new</span> <span class="n">Document</span><span class="p">(</span><span class="n">map</span><span class="p">));</span>
            <span class="p">}</span>
            <span class="k">else</span> <span class="p">{</span>
                <span class="n">collection</span><span class="p">.</span><span class="na">updateOne</span><span class="p">(</span><span class="n">Filters</span><span class="p">.</span><span class="na">eq</span><span class="p">(</span><span class="s">"ip"</span><span class="p">,</span> <span class="n">ip</span><span class="p">),</span> <span class="k">new</span> <span class="n">Document</span><span class="p">(</span><span class="s">"$set"</span><span class="p">,</span><span class="k">new</span> <span class="n">Document</span><span class="p">(</span><span class="n">map</span><span class="p">)));</span>
            <span class="p">}</span>
        <span class="p">}</span><span class="k">catch</span><span class="p">(</span><span class="n">Exception</span> <span class="n">e</span><span class="p">){</span>
            <span class="n">System</span><span class="p">.</span><span class="na">err</span><span class="p">.</span><span class="na">println</span><span class="p">(</span> <span class="n">e</span><span class="p">.</span><span class="na">getClass</span><span class="p">().</span><span class="na">getName</span><span class="p">()</span> <span class="o">+</span> <span class="s">": "</span> <span class="o">+</span> <span class="n">e</span><span class="p">.</span><span class="na">getMessage</span><span class="p">()</span> <span class="p">);</span>
            <span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="kd">public</span> <span class="kd">static</span> <span class="n">Document</span> <span class="nf">query</span><span class="p">(</span><span class="n">String</span> <span class="n">key</span><span class="p">,</span> <span class="n">String</span> <span class="n">value</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="n">collection</span><span class="p">.</span><span class="na">find</span><span class="p">(</span><span class="n">eq</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)).</span><span class="na">first</span><span class="p">();</span>
    <span class="p">}</span>

    <span class="kd">public</span> <span class="kd">static</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Map</span><span class="o">&gt;</span> <span class="nf">find</span><span class="p">(</span><span class="n">BasicDBObject</span> <span class="n">queryObj</span><span class="p">,</span> <span class="kt">int</span> <span class="n">detail</span><span class="p">,</span> <span class="kt">int</span> <span class="n">limit</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">mongoDatabase</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">init</span><span class="p">();</span>
        <span class="p">}</span>

        <span class="n">List</span><span class="o">&lt;</span><span class="n">Map</span><span class="o">&gt;</span> <span class="n">result</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;&gt;</span><span class="p">();</span>

        <span class="n">FindIterable</span><span class="o">&lt;</span><span class="n">Document</span><span class="o">&gt;</span> <span class="n">cursor</span> <span class="o">=</span> <span class="n">collection</span><span class="p">.</span><span class="na">find</span><span class="p">(</span><span class="n">queryObj</span><span class="p">);</span>
        <span class="n">cursor</span><span class="p">.</span><span class="na">sort</span><span class="p">(</span><span class="k">new</span> <span class="n">BasicDBObject</span><span class="p">(</span><span class="s">"time"</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">));</span>
        <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">String</span><span class="p">.</span><span class="na">valueOf</span><span class="p">(</span><span class="n">limit</span><span class="p">).</span><span class="na">equals</span><span class="p">(</span><span class="s">"-1"</span><span class="p">))</span> <span class="p">{</span>
            <span class="n">cursor</span><span class="p">.</span><span class="na">limit</span><span class="p">(</span><span class="n">limit</span><span class="p">);</span>
        <span class="p">}</span>

        <span class="k">for</span><span class="p">(</span><span class="n">Document</span> <span class="n">ret</span> <span class="p">:</span> <span class="n">cursor</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">Map</span> <span class="n">data</span> <span class="o">=</span> <span class="n">JSON</span><span class="p">.</span><span class="na">parseObject</span><span class="p">(</span><span class="n">ret</span><span class="p">.</span><span class="na">toJson</span><span class="p">());</span>
            <span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">data</span><span class="p">);</span>

            <span class="k">if</span><span class="p">(</span><span class="n">detail</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">Map</span> <span class="n">temp</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="p">();</span>
                <span class="n">temp</span><span class="p">.</span><span class="na">put</span><span class="p">(</span><span class="s">"id"</span><span class="p">,</span> <span class="n">data</span><span class="p">.</span><span class="na">get</span><span class="p">(</span><span class="s">"id"</span><span class="p">));</span>
                <span class="n">temp</span><span class="p">.</span><span class="na">put</span><span class="p">(</span><span class="s">"name"</span><span class="p">,</span> <span class="n">data</span><span class="p">.</span><span class="na">get</span><span class="p">(</span><span class="s">"name"</span><span class="p">));</span>
                <span class="n">temp</span><span class="p">.</span><span class="na">put</span><span class="p">(</span><span class="s">"type"</span><span class="p">,</span> <span class="n">data</span><span class="p">.</span><span class="na">get</span><span class="p">(</span><span class="s">"type"</span><span class="p">));</span>
                <span class="n">temp</span><span class="p">.</span><span class="na">put</span><span class="p">(</span><span class="s">"time"</span><span class="p">,</span> <span class="n">data</span><span class="p">.</span><span class="na">get</span><span class="p">(</span><span class="s">"time"</span><span class="p">));</span>
                <span class="n">temp</span><span class="p">.</span><span class="na">put</span><span class="p">(</span><span class="s">"tag"</span><span class="p">,</span> <span class="n">data</span><span class="p">.</span><span class="na">get</span><span class="p">(</span><span class="s">"tag"</span><span class="p">));</span>
                <span class="n">temp</span><span class="p">.</span><span class="na">put</span><span class="p">(</span><span class="s">"count"</span><span class="p">,</span> <span class="n">data</span><span class="p">.</span><span class="na">get</span><span class="p">(</span><span class="s">"count"</span><span class="p">));</span>
                <span class="n">temp</span><span class="p">.</span><span class="na">put</span><span class="p">(</span><span class="s">"attack_list"</span><span class="p">,</span> <span class="n">data</span><span class="p">.</span><span class="na">get</span><span class="p">(</span><span class="s">"attack_list"</span><span class="p">));</span>
                <span class="n">result</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="n">temp</span><span class="p">);</span>
            <span class="p">}</span>
            <span class="k">else</span> <span class="p">{</span>
                <span class="n">result</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="n">data</span><span class="p">);</span>
            <span class="p">}</span>

        <span class="p">}</span>
        <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">//        BasicDBList condList = new BasicDBList();</span>
<span class="c1">//        BasicDBObject ageObj = new BasicDBObject("time",new BasicDBObject("$gt", 1542603240));</span>
<span class="c1">//        BasicDBObject sexObj = new BasicDBObject("type", 3);</span>
<span class="c1">//        condList.addAll((Collection&lt;?&gt;) ageObj);</span>
<span class="c1">//        condList.addAll((Collection&lt;?&gt;) sexObj);</span>
<span class="c1">//        BasicDBObject cond= new BasicDBObject();</span>
<span class="c1">//        cond.put("$and", condList);</span>

        <span class="n">List</span><span class="o">&lt;</span><span class="n">DBObject</span><span class="o">&gt;</span> <span class="n">cond</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;</span><span class="n">DBObject</span><span class="o">&gt;</span><span class="p">();</span>
<span class="c1">//        BasicDBObject ageObj = new BasicDBObject("time",new BasicDBObject("$gt", 1542603240));</span>
        <span class="n">BasicDBObject</span> <span class="n">sexObj</span> <span class="o">=</span> <span class="k">new</span> <span class="n">BasicDBObject</span><span class="p">(</span><span class="s">"type"</span><span class="p">,</span> <span class="mi">3</span><span class="p">);</span>
<span class="c1">//        cond.add(ageObj);</span>
        <span class="n">cond</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="n">sexObj</span><span class="p">);</span>
        <span class="n">BasicDBObject</span> <span class="n">andQuery</span> <span class="o">=</span> <span class="k">new</span> <span class="n">BasicDBObject</span><span class="p">();</span>
        <span class="n">andQuery</span><span class="p">.</span><span class="na">put</span><span class="p">(</span><span class="s">"$or"</span><span class="p">,</span> <span class="n">cond</span><span class="p">);</span>
        <span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">andQuery</span><span class="p">.</span><span class="na">toString</span><span class="p">());</span>

        <span class="n">MongoDBUtil</span><span class="p">.</span><span class="na">find</span><span class="p">(</span><span class="n">andQuery</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
     </div>
     <h2 id="_1">
      参考链接
     </h2>
     <ul>
      <li>
       <a href="https://www.cnblogs.com/luoaz/p/4691639.html">
        java MongoDB查询（一）简单查询
       </a>
      </li>
      <li>
       <a href="https://www.cnblogs.com/luoaz/p/4692716.html">
        java MongoDB查询（二）复杂查询
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/aboycoder/article/details/48358365">
        java中mongodb的查询及排序
       </a>
      </li>
      <li>
       <a href="http://mongodb.github.io/mongo-java-driver/3.9/driver/tutorials/connect-to-mongodb/">
        MongoDB Driver Tutorials
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/itachi85/article/details/50510124">
        设计模式（二）单例模式的七种写法
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/hqzxsc2006/article/details/47447997">
        MongoDB查询操作限制返回字段的方法
       </a>
      </li>
      <li>
       <a href="https://www.jb51.net/article/133539.htm">
        Java操作MongoDB数据库方法详解
       </a>
      </li>
      <li>
       <a href="http://www.voidcn.com/article/p-kaoovgif-bqs.html">
        Java通过mongo-java-driver-3.0+分页查询mongodb详解
       </a>
      </li>
      <li>
       <a href="https://www.cnblogs.com/woshimrf/p/mongodb-pagenation-performance.html">
        MongoDB分页的Java实现和分页需求的思考
       </a>
      </li>
     </ul>
    </article>
   </div>
   <!-- Footer -->
   <footer id="footer">
    <p class="copyright">
     © Untitled. Design:
     <a href="https://html5up.net">
      HTML5 UP
     </a>
     .
    </p>
   </footer>
  </div>
  <!-- BG -->
  <div id="bg">
  </div>
  <!-- Scripts -->
  <script src="../assets/js/jquery.min.js">
  </script>
  <script src="../assets/js/browser.min.js">
  </script>
  <script src="../assets/js/breakpoints.min.js">
  </script>
  <script src="../assets/js/util.js">
  </script>
  <script src="../assets/js/main.js">
  </script>
 </body>
</html>
